package com.jeff.rps;

import com.jeff.rps.ui.MainScreen;

public class GameFactory
{

  public Game createGame(String p1class, String p2class, int numRounds)
      throws Exception
  {
    Player p1 = makePlayer(p1class);
    Player p2 = makePlayer(p2class);

    return new Game(p1, p2, numRounds);
  }

  public void run(Game game)
  {
    while (!game.isOver())
      game.nextTurn();
  }

  private Player makePlayer(String classname) throws Exception
  {
    Class<?> clas = Class.forName(classname);
    return (Player) clas.newInstance();
  }

  public static void main(String[] args) throws Exception
  {
    GameFactory factory = new GameFactory();
    Game game = factory.createGame(
        "com.jeff.rps.sample.AlwaysRock", "com.jeff.rps.sample.AlwaysScissors",
        3);

    MainScreen screen = new MainScreen(game);
    screen.setVisible(true);

    factory.run(game);
  }

}
